﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlacialComponents.Controls;

namespace WebBrowserTest
{
    public partial class AnalyzeProgressForm : Form
    {
        private Form ParentForm { get; set; }
        public event EventHandler OnAnalyzeCompleted;
        public bool BtnCloseVisible
        {
            set
            {
                this.button1.Invoke(new MethodInvoker(()=>button1.Enabled = value));
            }
        }
        public bool IsInDetailsMode
        {
            set
            {
                lblDetailsAnalyzeInfo.Invoke(new MethodInvoker(() => this.lblDetailsAnalyzeInfo.Visible = value));
                pbDetailsAnalyze.Invoke(new MethodInvoker(() => this.pbDetailsAnalyze.Visible = value));
            }
        }

        public string AnalyzedDomain
        {
            get { return lblAnalyzedDomain.Text; }
            set { lblAnalyzedDomain.Invoke(new MethodInvoker(() => lblAnalyzedDomain.Text = value)); }
        }
         
        public string WebAnalyzed
        {
            get { return lblWebAnalyze.Text; }
            set { lblWebAnalyze.Invoke(new MethodInvoker(() => lblWebAnalyze.Text = value)); }
        }

        public string DBAnalyzed
        {
            get { return lblAnalyzeFromDB.Text; }
            set
            {
                lblAnalyzeFromDB.Invoke(new MethodInvoker(() => lblAnalyzeFromDB.Text = value));
                
            }

        }
        public AnalyzeProgressForm(Form parent)
        {
            ParentForm = parent;
          
            InitializeComponent();
            InitListBox();
            pbDetailsAnalyze.Value = 0;
            pbDetailsAnalyze.Maximum = 10;
        }

        public ListViewRowItem AddNewRow(string domainName)
        {
            ListViewRowItem._domainsCount++;

            ListViewRowItem rowItem = new ListViewRowItem();
            GLItem mainItem = new GLItem();
            mainItem.BackColor = System.Drawing.Color.White;
            mainItem.ForeColor = System.Drawing.Color.Black;
            mainItem.RowBorderColor = System.Drawing.Color.Black;
            mainItem.RowBorderSize = 0;


            Label domainLabel = new Label();
            Label doneInfoLabel = new Label();
            domainLabel.Text = domainName;
            

            ProgressBar analyzeProgress = new ProgressBar();
            analyzeProgress.Value = 0;
            analyzeProgress.Maximum = 11;
            rowItem.AnalyzeProgress = analyzeProgress;
            rowItem.DomainLabel = domainLabel;
            rowItem.DoneInfoLabel = doneInfoLabel;
            rowItem.CurrentRow = mainItem;

            GLSubItem domainSubitem = new GLSubItem();
            domainSubitem.Control = domainLabel;

            GLSubItem progressSubitem = new GLSubItem();
            progressSubitem.Control = analyzeProgress;

            GLSubItem statusSubitem = new GLSubItem();
            statusSubitem.Control = doneInfoLabel;
           

            mainItem.SubItems.AddRange(new[]{domainSubitem,progressSubitem,statusSubitem});
            glacialList1.Items.Add(mainItem);

            return rowItem;
        }
        public DetailsProgressItems GetDetailsProgressItems()
        {
            
            var dpi = new DetailsProgressItems
                                           {
                                               DetailsAnalyzeProgressBar = this.pbDetailsAnalyze,
                                               InfoLabel = lblDetailsAnalyzeInfo
                                           };
            return dpi;
        }


        private void InitListBox()
        {
            GLColumn glColumn1 = new GLColumn();
            GLColumn glColumn2 = new GLColumn();
            GLColumn glColumn3 = new GLColumn();

            glacialList1.AllowColumnResize = false;
            glacialList1.AllowMultiselect = false;
            glacialList1.AlternatingColors = true;
            glacialList1.AutoHeight = true;
            glacialList1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            glacialList1.BackgroundStretchToFit = true;

            glColumn1.ActivatedEmbeddedType = GlacialComponents.Controls.GLActivatedEmbeddedTypes.None;
            glColumn1.Name = "domainColumn";
            glColumn1.Text = "Domena";
            glColumn1.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            glColumn1.Width = glacialList1.Width/2;
            

            glColumn2.ActivatedEmbeddedType = GlacialComponents.Controls.GLActivatedEmbeddedTypes.None;
            glColumn2.Name = "progressColumn";
            glColumn2.Text = "Postęp";
            glColumn2.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            glColumn2.Width = glacialList1.Width / 4;

            glColumn3.ActivatedEmbeddedType = GlacialComponents.Controls.GLActivatedEmbeddedTypes.None;
            glColumn3.Name = "statusColumn";
            glColumn3.Text = "Status";
            glColumn3.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            glColumn3.Width = (glacialList1.Width / 4)-3;

            glacialList1.Columns.AddRange(new []{glColumn1,glColumn2, glColumn3});
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pbDetailsAnalyze.Invoke(new MethodInvoker(() => pbDetailsAnalyze.Value = 0));
            glacialList1.Items.Clear();
            button1.Enabled = false;
            ParentForm.Invoke(new MethodInvoker(() => ParentForm.Enabled = true));
            this.Close();
            if (OnAnalyzeCompleted != null)
            {
                OnAnalyzeCompleted.Invoke(this, new EventArgs());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }

    #region Helper classes
    public class ListViewRowItem
    {
        public static int _domainsCount = 0;

        public static int _globalProgressValue = 0;

        public Label DomainLabel { get; set; }

        public ProgressBar AnalyzeProgress { get; set; }

        public Label DoneInfoLabel { get; set; }

        public GLItem CurrentRow { get; set; }



        public void UpdateProgressBar(int value)
        {
            _globalProgressValue += value;
            this.AnalyzeProgress.Invoke(new MethodInvoker(() => this.AnalyzeProgress.Increment(value)),value);
        }

        public void ProcessUpdateStatusLabel()
        {
            this.DoneInfoLabel.Invoke(new MethodInvoker(() => this.DoneInfoLabel.Text = "Zakończono"));
        }
    }

    public class DetailsProgressItems
    {
        public Label InfoLabel { get; set; }

        public ProgressBar DetailsAnalyzeProgressBar { get; set; }

        public Form MainForm { get; set; }

        public void UpdateProgress(int value)
        {
            DetailsAnalyzeProgressBar.Invoke(new MethodInvoker(()=>DetailsAnalyzeProgressBar.Increment(value)));
        }

        internal void SetMaxProgressValue(int p)
        {
            DetailsAnalyzeProgressBar.Invoke(new MethodInvoker(()=>DetailsAnalyzeProgressBar.Maximum = p));
        }
    }
    #endregion
}
